@extends('develop.layoutList')

@section('css')
    <style>
        ul li a {
            color: #666666;
        }
        ul li a:hover {
            color: #666666;
        }
        ul li a:visited {
            color: #666666;
        }
    </style>
@endsection

@section('title')
    <li @if ($task_type == 1) class="cur" @endif  id="li-1">
        <a href="/lar/develop/task/?task_type=1"><span>一分钟任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 2) class="cur" @endif id="li-2">
        <a href="/lar/develop/task/?task_type=2"><span>五分钟任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 3) class="cur" @endif id="li-3">
        <a href="/lar/develop/task/?task_type=3"><span>十分钟任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 4) class="cur" @endif id="li-4">
        <a href="/lar/develop/task/?task_type=4"><span>半小时任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 5) class="cur" @endif id="li-5">
        <a href="/lar/develop/task/?task_type=5"><span>一小时任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 6) class="cur" @endif id="li-6">
        <a href="/lar/develop/task/?task_type=6"><span>0点任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 7) class="cur" @endif id="li-7">
        <a href="/lar/develop/task/?task_type=7"><span>1点任务</span></a>
    </li>
    <li style="margin-left:15px;" @if ($task_type == 8) class="cur" @endif id="li-8">
        <a href="/lar/develop/task/?task_type=8"><span>3点任务</span></a>
    </li>
@endsection

@section('btn')
    <button class="btn btn-primary" type="button" onclick="sysTask.edit();">添加</button>
@endsection

@section('search')

    <input type="text" name="task_name" value=""  class="form-control w150" placeholder="请输入任务名称">
    <input type="text" name="task_link" value=""  class="form-control" style="width:300px" placeholder="请输入任务链接">

@endsection

@section('js')
    <script>

    var bootstrap_table_ajax_url = '/lar/develop/task/search?task_type={{$task_type}}';
    bootstrap_table({
        sortName: 'task_id',
        sortOrder: 'DESC',
        columns: [
            {title: '操作', field: 'operation', align: 'center',width: '200px'},
            {title: '任务名称', field: 'task_name', align: 'left'},
            {title: '任务链接', field: 'task_link', align: 'left'},
            {title: 'act值', field: 'task_act_value', align: 'left'},
            {title: '状态', field: 'task_status', align: 'left'}
        ]
    });

    var sysTask = {

        task_type:'',
        task_id:'',
        status_task_id:'',
        status_task_status:'',
        del_task_id:'',
        show_task_id:'',
        load_index :'',

        //添加or编辑任务
        edit: function( task_id ) {

            var html ='<form id="addForm" onsubmit="return false;" class="form-horizontal" role="form" style="margin-top: 10px">';
            html +='<div class="form-group"  >';
            html +='<label class="col-sm-3 control-label" for="task_name"><span class="red pr5">*</span>任务名称：</label>';
            html +='<div class="col-sm-7">';
            html +='<input class="form-control w300"  type="text" id="task_name" name="task_name"  placeholder="请输入任务名称" />';
            html +='</div>';
            html +='</div>';
            html +='<div class="form-group"  >';
            html +='<label class="col-sm-3 control-label" for="task_link" ><span class="red pr5">*</span>任务链接：</label>';
            html +='<div class="col-sm-7">';
            html +='<input class="form-control w300"  type="text" id="task_link" name="task_link"  placeholder="请输入任务链接" />';
            html +='</div>';
            html +='</div>';
            html +='<div class="form-group"  >';
            html +='<label class="col-sm-3 control-label" for="task_act_value"><span class="red pr5">*</span>act值：</label>';
            html +='<div class="col-sm-7">';
            html +='<input class="form-control w300"  type="text" id="task_act_value" name="task_act_value"  placeholder="请输入act值" />';
            html +='</div>';
            html +='</div>';
            html +='<input type="hidden" id="task_id" name="task_id">';
            html +='<input type="hidden" name="task_type" value="{{$task_type}}" id="task_type">';
            html +='</form>';

            sysTask.reset();

            var name = "";

            if ( task_id ) {
                name = "修改任务";
            } else {
                name = "添加任务";
            }

            layer.open({
                type:1,
                title: name,
                content: html,
                offset:'50px',
                area:'550px',
                btn: ['确定', '取消'],
                yes: function(){
                    sysTask.store();
                },
                btn2: function(){
                    sysTask.reset();
                }
            });

            if ( task_id ) {
                sysTask.task_id = task_id;

                E.ajax({
                    type: 'GET',
                    url: "/lar/develop/task/get/"+task_id,
                    data: {
                        task_id: task_id
                    },
                    success: function ( o ) {

                        if( o.code == 200 ) {
                            $('#task_id').val(o.data.task_id);
                            $('#task_name').val(o.data.task_name);
                            $('#task_link').val(o.data.task_link);
                            $('#task_act_value').val(o.data.task_act_value);
                        }

                    }
                });

            }
        },

        //编辑任务成功后 重置弹出层input框
        reset: function(){
            $('#task_name').val("");
            $('#task_id').val("");
            $('#task_link').val("");
            $('#task_act_value').val("");
        },

        //保存数据
        store: function (){
            this.dt = E.getFormValues("addForm");
            var error_msg = "";

            if (this.dt.task_name == '') {
                error_msg += "任务名不能为空<br>";
            }
            if (E.isEmpty(this.dt.task_link)) {
                error_msg += "任务链接不能为空<br>";
            }
            if (E.isEmpty(this.dt.task_act_value)) {
                error_msg += "act值不能为空<br>";
            }

            if (error_msg != "") {
                layer.alert(error_msg,{icon:2});
                return false;
            } else {
                if( sysTask.task_id != "" ){
                    var name = "修改";
                }else{
                    var name = "添加";
                }

                layer.confirm('您确认'+name+'该任务吗?',{icon:3}, function () {
                    var dt = E.getFormValues("addForm");
                    E.ajax({
                        type:'get',
                        url: "/lar/develop/task/store",
                        data: dt,
                        success:function( o ) {
                            if( o.code == 200 ) {
                                layer.alert(o.message, {icon: 1, offset: '70px', time: 3000});
                                sysTask.load();
                                sysTask.reset();
                            }else{
                                layer.alert(o.message,{icon:2});
                            }
                        }
                    });
                });
            }
        },

        //运行or暂停任务确认
        changeStatus:function( task_status, task_id ){
            sysTask.status_task_id = task_id;
            sysTask.status_task_status = task_status;

            if( task_status == 1 ){
                var name = "暂停";
            } else {
                var name = "运行";
            }

            layer.confirm('您确认'+name+'该任务吗?', {icon:3}, function (index) {
                layer.close( index );
                E.ajax({
                    type:'get',
                    url: "/lar/develop/task/status",
                    data: {
                        task_id:sysTask.status_task_id,
                        task_status:sysTask.status_task_status
                    },
                    success:function( o ) {
                        if( o.code == 200 ) {
                            layer.alert(o.message, {icon: 1, offset: '70px', time: 2000});
                            sysTask.load();
                        }else{
                            layer.alert(o.message,{icon:2});
                        }
                    }
                });
            });
        },
        //删除任务
        del:function( task_id ){
            sysTask.del_task_id = task_id;

            layer.confirm('您确认删除该任务吗?',{icon:3}, function () {
                E.ajax({
                    type:'get',
                    url: "/lar/develop/task/delete/"+task_id,
                    data: {
                        task_id:sysTask.del_task_id
                    },
                    success: function ( o ) {
                        if( o.code == 200 ) {
                            layer.alert(o.message, {icon: 1, offset: '70px', time: 2000});
                            sysTask.load();
                        }else{
                            layer.alert(o.message,{icon:2});
                        }
                    }
                });
            });

        },

        //查询日志信息
        log: function( task_id ) {

            var load_index = layer.load();
            E.ajax({
                type:'get',
                url: "/lar/develop/task/log/"+task_id,
                data: {
                    task_id: task_id,
                    sortname: 'start_time',
                    sortorder: 'DESC',
                    rp: 8
                },
                success: function ( o ) {

                    //关闭等待层
                    layer.close(load_index);
                    layer.closeAll();

                    var html = '';
                    html += '<div style="height:390px;padding: 5px;">';
                    html += '<table class="table table-striped table-bordered table-hover">';
                    html += '<tbody>';
                    html += '<tr>';
                    html += '<th width="90" style="text-align: center;">任务执行时间</th>';
                    html += '<th width="90" style="text-align: center;">任务结束时间</th>';
                    html += '<th width="80">总耗时时间</th>';
                    html += '<th width="160">任务执行结果</th>';

                    html += '</tr>';
                    if(o.data){
                        $.each(o.data, function(k, v) {

                            html += '<tr>';
                            html += '<td style="text-align: center;">' + v.start_time + '</td>';
                            if( v.end_time ){
                                html += '<td style="text-align: center;">' + v.end_time + '</td>';
                            }else{
                                html += '<td></td>';
                            }

                            if( v.total_time ){
                                html += '<td>' + v.total_time + '&nbsp;s</td>';
                            }else{
                                html += '<td></td>';
                            }

                            if( v.result ){
                                html += '<td>' + v.result + '</td>';
                            }else{
                                html += '<td></td>';
                            }

                            html += '</tr>';

                        });
                    }else{
                        html += '<tr>';
                        html += '<td style="text-align: center" colspan="5">没有找到记录</td>';
                        html += '</tr>';
                    }

                    html += '</tbody>';
                    html += '</table>';
                    html += '</div>';

                    if (o.paging) {
                        html += o.paging;
                    }

                    layer.open({
                        type:1,
                        content: html,
                        offset:'20px',
                        title: '任务日志列表',
                        area:['800px','450px']
                    });
                }
            });
        },
        //重新加载表格
        load: function() {
            //刷新表格
            $('#table').bootstrapTable('refresh');
        },

        clear: function () {
            $("#task_name").val("");
            $("#task_link").val("");
            this.search();
        }
    }
    </script>

@endsection